﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGCourt.Logic.Common;
using KMFrame.Core;
using NBearLite;
using LGCourt.Logic.NBL;
using LGCourt.Logic.NBL.DBEntities;
using System.Data;

namespace LGCourt.Logic.BLL
{
    public class ColInfo : LGCourtLogic
    {
        #region 栏目信息
        /// <summary>
        /// 资讯信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetNewsInfoPage(QueryCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetCondition(condition).Select.ToList<LGFY_Platform_Articles>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.LGFY_Platform_Articles, LGCourtColumns.LGFY_Platform_Articles.ID, GetCondition(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }


        public List<LGFY_Platform_Articles> GetNewsInfoTop(QueryCondition condition)
        {
            return GetCondition(condition).Select.ToList<LGFY_Platform_Articles>();
        }

        public LGFY_Platform_Articles GetNewsInfo(QueryCondition condition)
        {
            LGFY_Platform_Articles result= NBLData.Select(LGCourtColumns.LGFY_Platform_Articles)
                .Where(LGCourtColumns.LGFY_Platform_Articles.ID == condition.id)
                .ToSingleObject<LGFY_Platform_Articles>();
            UpdateArticlesBrowseCount(result);
            return result;
        }

        public bool UpdateArticlesBrowseCount(LGFY_Platform_Articles param)
        {
            bool resutl = false;
            UpdateSqlSection update = NBLData.Update(LGCourtColumns.LGFY_Platform_Articles)
                .AddColumn(LGCourtColumns.LGFY_Platform_Articles.Hit, param.Hit.GetValueOrDefault(0) + 1)
               .Where(LGCourtColumns.LGFY_Platform_Articles.ID == param.ID);
            int ri = update.Execute();
            if (ri == 1)
                resutl = true;
            return resutl;
        }

        public LGFY_Platform_Articles GetNewsContentInfo(QueryCondition condition)
        {
            LGFY_Platform_Articles result = new LGFY_Platform_Articles();
            ExpressionClip[] zjjhColumns =
       new ExpressionClip[] { 
                LGCourtColumns.LGFY_Platform_Articles.ID,
                LGCourtColumns.LGFY_Platform_Articles.Title,
                LGCourtColumns.LGFY_Platform_Articles.Text,
                LGCourtColumns.LGFY_Platform_Articles.PubDate,
                LGCourtColumns.LGFY_Platform_Articles.Comefrom,
                LGCourtColumns.LGFY_Platform_Articles.ClassID,
                LGCourtColumns.LGFY_Platform_Articles.Hit,
                LGCourtColumns.LGFY_Platform_Articles.KeyWord,
            };

            DataSet ds = NBLData.Select(LGCourtColumns.LGFY_Platform_Articles)
           .Where(LGCourtColumns.LGFY_Platform_Articles.ID == condition.id)
           .ToDataSet();  //ToList<pZJJHB>().FirstOrDefault();
            if (ds == null || ds.Tables == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                return null;
            DataRow dr = ds.Tables[0].Rows[0];
            result.ID = dr["ID"] == DBNull.Value ? 0 : int.Parse(dr["ID"].ToString());
            result.Text = dr["Text"].ToString();
            result.PubDate = dr["PubDate"] == DBNull.Value ? DateTime.Now : DateTime.Parse(dr["PubDate"].ToString());
            result.Title = dr["Title"] == DBNull.Value ? "" : dr["Title"].ToString();
            result.Comefrom = dr["Comefrom"] == DBNull.Value ? "" : dr["Comefrom"].ToString();
            result.ClassID = dr["ClassID"] == DBNull.Value ? 0 : int.Parse(dr["ClassID"].ToString());

            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private ConditionCollection GetCondition(QueryCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.LGFY_Platform_Articles.ID, 
                LGCourtColumns.LGFY_Platform_Articles.ClassID, 
                LGCourtColumns.LGFY_Platform_Articles.Title,
                LGCourtColumns.LGFY_Platform_Articles.Comefrom,
                LGCourtColumns.LGFY_Platform_Articles.PubDate,
            };
            cdtColl.SelectColumns = selectColumns;

            //vPortalCategory category = GetCategoryInfo(condition);
            WhereClip where = LGCourtColumns.LGFY_Platform_Articles.ClassID == condition.ClassID;
            //WhereClip where = LGCourtColumns.LGFY_Platform_Articles.ClassID == (category == null ? 0 : category.ClassID);
            if (!string.IsNullOrEmpty(condition.from))
                where = where && LGCourtColumns.LGFY_Platform_Articles.Comefrom.Like(condition.from + "%");
            if (!string.IsNullOrEmpty(condition.keyword))
                where = where && (LGCourtColumns.LGFY_Platform_Articles.Title.Like("%" + condition.keyword + "%") || LGCourtColumns.LGFY_Platform_Articles.KeyWord.Like("%" + condition.keyword + "%"));
            if (condition.BegingDate.Year > 2000)
            {
                where = where && LGCourtColumns.LGFY_Platform_Articles.PubDate >= condition.BegingDate;
            }
            if (condition.EndDate.Year > 2000)
            {
                where = where && LGCourtColumns.LGFY_Platform_Articles.PubDate <= condition.EndDate;
            }

            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.LGFY_Platform_Articles)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.LGFY_Platform_Articles.ID)
                .OrderBy(LGCourtColumns.LGFY_Platform_Articles.TopSize.Asc, LGCourtColumns.LGFY_Platform_Articles.PubDate.Desc);

            return cdtColl;
        }
        #endregion

        #region 图片信息

        /// <summary>
        /// 图片信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetPicInfoPage(QueryCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetPicCondition(condition).Select.ToList<LGFY_Platform_NewsOfPic>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.LGFY_Platform_NewsOfPic, LGCourtColumns.LGFY_Platform_NewsOfPic.ID, GetPicCondition(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }


        public List<LGFY_Platform_NewsOfPic> GetPicInfoTop(QueryCondition condition)
        {
            return GetPicCondition(condition).Select.ToList<LGFY_Platform_NewsOfPic>();
        }

        public LGFY_Platform_NewsOfPic GetPicInfo(QueryCondition condition)
        {
            LGFY_Platform_NewsOfPic result=NBLData.Select(LGCourtColumns.LGFY_Platform_NewsOfPic)
                .Where(LGCourtColumns.LGFY_Platform_NewsOfPic.ID == condition.id)
                .ToSingleObject<LGFY_Platform_NewsOfPic>();
            UpdateNewOfPicBrowseCount(result);
            return result;
        }

        public bool UpdateNewOfPicBrowseCount(LGFY_Platform_NewsOfPic param)
        {
            bool resutl = false;

            UpdateSqlSection update = NBLData.Update(LGCourtColumns.LGFY_Platform_NewsOfPic)
                .AddColumn(LGCourtColumns.LGFY_Platform_NewsOfPic.Hit, param.Hit.GetValueOrDefault(0) + 1)
               .Where(LGCourtColumns.LGFY_Platform_NewsOfPic.ID == param.ID);


            int ri = update.Execute();
            if (ri == 1)
                resutl = true;
            return resutl;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private ConditionCollection GetPicCondition(QueryCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.LGFY_Platform_NewsOfPic.ID, 
                LGCourtColumns.LGFY_Platform_NewsOfPic.Title,
                LGCourtColumns.LGFY_Platform_NewsOfPic.Comefrom,
                LGCourtColumns.LGFY_Platform_NewsOfPic.PubDate,
                LGCourtColumns.LGFY_Platform_NewsOfPic.Pic,
            };
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = WhereClip.All;
            if (!string.IsNullOrEmpty(condition.from))
                where = where && LGCourtColumns.LGFY_Platform_NewsOfPic.Comefrom.Like(condition.from + "%");
            if (!string.IsNullOrEmpty(condition.keyword))
                where = where && (LGCourtColumns.LGFY_Platform_Articles.Title.Like("%" + condition.keyword + "%") || LGCourtColumns.LGFY_Platform_Articles.KeyWord.Like("%" + condition.keyword + "%"));
            if (condition.BegingDate.Year > 2000)
            {
                where = where && LGCourtColumns.LGFY_Platform_NewsOfPic.PubDate >= condition.BegingDate;
            }
            if (condition.EndDate.Year > 2000)
            {
                where = where && LGCourtColumns.LGFY_Platform_NewsOfPic.PubDate <= condition.EndDate;
            }
            if (condition.IsPicRoll)
            {
                where = where && LGCourtColumns.LGFY_Platform_NewsOfPic.IsShow == 1;
            }

            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.LGFY_Platform_NewsOfPic)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.LGFY_Platform_NewsOfPic.ID)
                .OrderBy(LGCourtColumns.LGFY_Platform_NewsOfPic.TopSize.Asc, LGCourtColumns.LGFY_Platform_NewsOfPic.PubDate.Desc);

            return cdtColl;
        }
        #endregion

        #region 部门栏目信息

        /// <summary>
        /// 获取部门栏目信息列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<V_DICT_SOURCE> GetDeptSourceList(DeptClassCondition condition)
        {
            WhereClip where=WhereClip.All;
            if(!string.IsNullOrEmpty(condition.Source))
                where=where && LGCourtColumns.V_DICT_SOURCE.Source == condition.Source ;
             if(!string.IsNullOrEmpty(condition.Name))
                 where=where && LGCourtColumns.V_DICT_SOURCE.Name == condition.Name ;
             if (condition.ClassID>0)
                 where = where && LGCourtColumns.V_DICT_SOURCE.ID == condition.ClassID;

            return NBLData.Select(LGCourtColumns.V_DICT_SOURCE)
              .Where(where)
              .OrderBy(LGCourtColumns.V_DICT_SOURCE.ID.Asc)
              .ToList<V_DICT_SOURCE>();
        }

        /// <summary>
        /// 获取部门栏目信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public V_DICT_SOURCE GetDeptSourceInfo(DeptClassCondition condition)
        {
            WhereClip where = WhereClip.All;
            if (!string.IsNullOrEmpty(condition.Source))
                where = where && LGCourtColumns.V_DICT_SOURCE.Source == condition.Source;
            if (!string.IsNullOrEmpty(condition.Name))
                where = where && LGCourtColumns.V_DICT_SOURCE.Name == condition.Name;
            if (condition.ClassID > 0)
                where = where && LGCourtColumns.V_DICT_SOURCE.ID == condition.ClassID;

            return NBLData.Select(LGCourtColumns.V_DICT_SOURCE)
              .Where(where)
              .OrderBy(LGCourtColumns.V_DICT_SOURCE.ID.Asc)
              .ToSingleObject<V_DICT_SOURCE>();
        }

        /// <summary>
        /// 部门栏目信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetDeptColInfoPage(DeptCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetDeptColCondition(condition).Select.ToList<LGFY_Platform_Bmxx>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.LGFY_Platform_Bmxx, LGCourtColumns.LGFY_Platform_Bmxx.ID, GetDeptColCondition(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        public List<LGFY_Platform_Bmxx> GetDeptColInfoTop(DeptCondition condition)
        {
            return GetDeptColCondition(condition).Select.ToList<LGFY_Platform_Bmxx>();
        }

        public LGFY_Platform_Bmxx GetDeptColInfo(DeptCondition condition)
        {
            LGFY_Platform_Bmxx result= NBLData.Select(LGCourtColumns.LGFY_Platform_Bmxx)
                .Where(LGCourtColumns.LGFY_Platform_Bmxx.ID == condition.id)
                .ToSingleObject<LGFY_Platform_Bmxx>();
            UpdateBmxxBrowseCount(result);
            return result;
        }

        public bool UpdateBmxxBrowseCount(LGFY_Platform_Bmxx param)
        {
            bool resutl = false;

            UpdateSqlSection update = NBLData.Update(LGCourtColumns.LGFY_Platform_Bmxx)
                .AddColumn(LGCourtColumns.LGFY_Platform_Bmxx.Hit, param.Hit.GetValueOrDefault(0) + 1)
               .Where(LGCourtColumns.LGFY_Platform_Bmxx.ID == param.ID);
            int ri = update.Execute();
            if (ri == 1)
                resutl = true;
            return resutl;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private ConditionCollection GetDeptColCondition(DeptCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.LGFY_Platform_Bmxx.ID, 
                LGCourtColumns.LGFY_Platform_Bmxx.DeptID,
                LGCourtColumns.LGFY_Platform_Bmxx.ClassID,
                LGCourtColumns.LGFY_Platform_Bmxx.Title,
                LGCourtColumns.LGFY_Platform_Bmxx.Comefrom,
                LGCourtColumns.LGFY_Platform_Bmxx.PubDate,
                LGCourtColumns.LGFY_Platform_Bmxx.Pic,
            };
            cdtColl.SelectColumns = selectColumns;

            //vPortalCategory category = GetCategoryInfo(condition);
            WhereClip where = LGCourtColumns.LGFY_Platform_Bmxx.DeptID == condition.DeptID;
            //WhereClip where = LGCourtColumns.LGFY_Platform_Bmxx.ClassID == (category == null ? 0 : category.ClassID);
            if (condition.ClassID>0)
                where = where && LGCourtColumns.LGFY_Platform_Bmxx.ClassID == condition.ClassID;
            if(condition.IsPicRoll)
                where = where && LGCourtColumns.LGFY_Platform_Bmxx.IsShow==1;
            if (!string.IsNullOrEmpty(condition.from))
                where = where && LGCourtColumns.LGFY_Platform_Bmxx.Comefrom.Like(condition.from + "%");
            if (!string.IsNullOrEmpty(condition.keyword))
                where = where && (LGCourtColumns.LGFY_Platform_Bmxx.Title.Like("%" + condition.keyword + "%") || LGCourtColumns.LGFY_Platform_Bmxx.KeyWord.Like("%" + condition.keyword + "%"));
            if (condition.BegingDate.Year > 2000)
            {
                where = where && LGCourtColumns.LGFY_Platform_Bmxx.PubDate >= condition.BegingDate;
            }
            if (condition.EndDate.Year > 2000)
            {
                where = where && LGCourtColumns.LGFY_Platform_Bmxx.PubDate <= condition.EndDate;
            }

            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.LGFY_Platform_Bmxx)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.LGFY_Platform_Bmxx.ID)
                .OrderBy(LGCourtColumns.LGFY_Platform_Bmxx.TopSize.Asc, LGCourtColumns.LGFY_Platform_Bmxx.PubDate.Desc);

            return cdtColl;
        }
        #endregion

        #region 部门简介
        /// <summary>
        /// 部门信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<LGFY_Platform_Index> GetDeptInfoList(DeptCondition condition)
        {
            return NBLData.Select(LGCourtColumns.LGFY_Platform_Index)
               .Where(LGCourtColumns.LGFY_Platform_Index.DeptID == condition.DeptID)               
               .ToList<LGFY_Platform_Index>();
        }

        /// <summary>
        /// 部门信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public LGFY_Platform_Index GetDeptIndexInfo(DeptCondition condition)
        {
            return NBLData.Select(LGCourtColumns.LGFY_Platform_Index)
               .Where(LGCourtColumns.LGFY_Platform_Index.DeptID == condition.DeptID)
               .ToSingleObject<LGFY_Platform_Index>();
        }

        /// <summary>
        /// 部门栏目名称列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<IF_DeptClass> GetDeptClassList(DeptCondition condition)
        {
            return NBLData.Select(LGCourtColumns.IF_DeptClass)
               .Where(LGCourtColumns.IF_DeptClass.DeptID == condition.DeptID)
               .OrderBy(LGCourtColumns.IF_DeptClass.ClassID.Asc)
               .ToList<IF_DeptClass>();
        }

        /// <summary>
        /// 部门栏目名称
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IF_DeptClass GetDeptClassInfo(DeptCondition condition)
        {
            return NBLData.Select(LGCourtColumns.IF_DeptClass)
               .Where(LGCourtColumns.IF_DeptClass.DeptID == condition.DeptID && LGCourtColumns.IF_DeptClass.ClassID == condition.ClassID)
               .ToSingleObject<IF_DeptClass>();
        }
        #endregion

    }

    public class QueryCondition : PageParam
    {
        public int ClassID { get; set; } //分类
        public string from { get; set; }
        public int id { get; set; } //信息ID
        public string keyword { get; set; }
        public DateTime BegingDate { get; set; }
        public DateTime EndDate { get; set; }
        public bool IsPicRoll { get; set; }
    }

    public class DeptCondition : PageParam
    {
        public int ClassID { get; set; } //分类
        public int DeptID { get; set; }//部门ID
        public string from { get; set; }
        public int id { get; set; } //信息ID
        public string keyword { get; set; }
        public DateTime BegingDate { get; set; }
        public DateTime EndDate { get; set; }
        public bool IsPicRoll { get; set; }
    }

    public class DeptClassCondition
    {
        public string Name { get; set; }
        public string Source { get; set; } //分类
        public int ClassID { get; set; } //分类
    }

    public class InfoItemResult
    {
        public int aid { get; set; }
        public string title { get; set; }
        public string from { get; set; }
        public DateTime createart { get; set; }
    }

    /// <summary>
    /// 首页栏目ID
    /// </summary>
    public static class HomeInfoClassID
    {
        /// <summary>
        /// 图片新闻
        /// </summary>
        public static int TPXX
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 时事要闻
        /// </summary>
        public static int SSXW
        {
            get
            {
                return 1;
            }
        }

        /// <summary>
        /// 法治聚焦
        /// </summary>
        public static int FZJJ
        {
            get
            {
                return 2;
            }
        }

        /// <summary>
        /// 文苑沙龙
        /// </summary>
        public static int WYSL
        {
            get
            {
                return 3;
            }
        }

        /// <summary>
        /// 专题研究
        /// </summary>
        public static int ZTYJ
        {
            get
            {
                return 7;
            }
        }

        /// <summary>
        /// 他山之石
        /// </summary>
        public static int TSZS
        {
            get
            {
                return 4;
            }
        }

        /// <summary>
        /// 书画摄影
        /// </summary>
        public static int SHSY
        {
            get
            {
                return 5;
            }
        }

        /// <summary>
        /// 文体生活
        /// </summary>
        public static int WTSH
        {
            get
            {
                return 6;
            }
        }

        /// <summary>
        /// 争鸣园地
        /// </summary>
        public static int ZMYD
        {
            get
            {
                return 8;
            }
        }

        /// <summary>
        /// 案例评析
        /// </summary>
        public static int ALPX
        {
            get
            {
                return 9;
            }
        }

        /// <summary>
        /// 优秀文书
        /// </summary>
        public static int YXWS
        {
            get
            {
                return 10;
            }
        }

    }

    /// <summary>
    /// 部门栏目ID
    /// </summary>
    public static class DeptInfoClassID
    {
        /// <summary>
        /// 部门动态-右一
        /// </summary>
        public static int BMDT
        {
            get
            {
                return 1;
            }
        }

        /// <summary>
        /// 重要文件-中一
        /// </summary>
        public static int ZYWJ
        {
            get
            {
                return 1;
            }
        }

        /// <summary>
        /// 政治学习-右二
        /// </summary>
        public static int ZZXX
        {
            get
            {
                return 1;
            }
        }

        /// <summary>
        /// 部门风采-中二
        /// </summary>
        public static int BMFC
        {
            get
            {
                return 1;
            }
        }

        /// <summary>
        /// 办公助手-右三
        /// </summary>
        public static int BGZS
        {
            get
            {
                return 1;
            }
        }
    }

}
